package com.example.hans.navigationbyviewpageandradiobutton;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class ChartActivity extends AppCompatActivity{
    private static Socket mSocket;
    private EditText editText;
    private TextView textView;
    private Button button;

    private ImageView imageView;
    private TextView contactName;
    private byte request[] = new byte[2048];

    private String host = "120.79.66.125";
    private int port = 9999;

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch(msg.what) {
                case 1: {
                    textView.setText(textView.getText().toString()+"\nserver say:"+msg.obj);
                    break;
                }
                default:
                    super.handleMessage(msg);
            }
        }
    };

    public void hideTheTitleBar() {
        if(getSupportActionBar() != null){
            getSupportActionBar().hide();
            getWindow().setFlags(
                    WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN
            );
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chart_view);

        hideTheTitleBar();

        Intent intent = getIntent();
        String name = intent.getStringExtra("name");

        button = (Button) findViewById(R.id.btn_submit);
        editText = (EditText) findViewById(R.id.editxt_write);
        textView = (TextView) findViewById(R.id.tv_show);
        imageView = findViewById(R.id.Img_back);
        contactName = findViewById(R.id.tv_showName);
        contactName.setText(name);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String etRequest = editText.getText().toString();
                editText.setText("");
                try {
                    OutputStream outputStream = mSocket.getOutputStream();
                    request = etRequest.getBytes();
                    outputStream.write(request,0,etRequest.length());
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        });

        new Thread(mSocketRunnable).start();

    }



    private Runnable mSocketRunnable = new Runnable() {
        @Override
        public void run() {
            try {
                mSocket = new Socket("120.79.66.125",9999);
                InputStream inputStream = mSocket.getInputStream();
                while (true) {
                    int length = inputStream.read(request);
                    String content = new String(request,0,length);
                    Message msg = new Message();
                    msg.what = 1;
                    msg.obj = content;
                    mHandler.sendMessage(msg);
                    if (content.equals("exit")) {
                        break;
                    }
                }
                mSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };

    public  static void actionStart(Context context,String name) {
        Intent intent = new Intent(context,ChartActivity.class);
        intent.putExtra("name",name);
        context.startActivity(intent);
    }
}
